#include "LayerHomeMiddle.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"

USING_NS_CC;
using namespace ui;


CLayerHomeMiddle::CLayerHomeMiddle(void) : m_Middle(nullptr), m_AnimalPage(nullptr), m_UserInfo(nullptr)
{

}


CLayerHomeMiddle::~CLayerHomeMiddle(void)
{
}

bool CLayerHomeMiddle::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	m_Middle = CSLoader::createNode("HomeMiddle.csb");
	setContentSize(m_Middle->getContentSize());

	m_AnimalPage = (cocos2d::ui::PageView*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Middle, 16);

	//ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 93);
	//tLoginB->addTouchEventListener(this, toucheventselector(CSceneLogin::onLogin));

	//auto node = CSLoader::createNode("HomeMiddle.csb");
	//m_Middle->addChild(node);

	addChild(m_Middle);

	return true;
}

void CLayerHomeMiddle::onEnter() {
	CLayerBase::onEnter();

	m_Director->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(CLayerHomeMiddle::initAnimalPage, this));
}

/*
void CLayerHomeMiddle::onExit() {
	CLayerBase::onExit();
}
*/

void CLayerHomeMiddle::update(float delta) {
	CLayerBase::update(delta);
}

void CLayerHomeMiddle::initAnimalPage() {
	m_CoverEffect.SetPageView(m_AnimalPage);
	m_CoverEffect.SetCallback(CC_CALLBACK_2(CLayerHomeMiddle::onCurAnimalClick, this));
}

void CLayerHomeMiddle::onCurAnimalClick(cocos2d::Ref* pSender, cocos2d::ui::Widget::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
		{
			ssize_t tCurIndex = m_AnimalPage->getCurPageIndex();
			if (tCurIndex == 0) {
				if (nullptr == m_UserInfo) {
					m_UserInfo = m_LayerManager->GetLayer(CLayerManager::LayerIUserInfo);
					cocos2d::Scene* tRunScene = m_SceneManager->getRunScene();
					tRunScene->addChild(m_UserInfo);
					m_UserInfo->setPosition(20.00, 300.00);
				} else
					m_UserInfo->setVisible(true);
			} else if (tCurIndex == 1) {
				
			} else {
				
			}
		}
		break;
	default:
		break;
	}
}

/*
void CLayerHomeMiddle::onOpenPack(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		
		break;
	}
}*/
